﻿//Textbox number only
function NumberOnly(e) {

    if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode == 8)) {
        return true;
    }
    else {
        return false;
    }
}

function NumberExceptZero(e) {

    if ((e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode == 8)) {
        return true;
    }
    else {
        return false;
    }
}

function DecimalOnly(event, Control) {
    if (event.keyCode != 8) {
        if (!(event.keyCode >= 48 && event.keyCode <= 57)) {
            var regex = /^[0-9]+\.?[0-9]*$/;
            var InputValue = String.fromCharCode(event.keyCode);
            var key = document.getElementById(Control).value;
            key = key + InputValue;
       
            if (!regex.test(key)) {
                return false;
            }
        }
    }
}

String.prototype.startsWith = function (str) {
    return (this.indexOf(str) === 0);
}

function CheckFirstNumber(id, str) {
    var txt = document.getElementById(id).value;
    if (txt.startsWith(str)) {
        document.getElementById(id).value = "";
    }
}

function ClearFormatNumber(id) {
    var txt = document.getElementById(id).value;
    if (txt.indexOf(",") != -1) {
        document.getElementById(id).value = "";
        var n = txt.split(",");
        for (var i = 0; i < n.length; i++) {
            document.getElementById(id).value += n[i];
        }
        SetCursorToTextEnd(id);
    }
}

function SetCursorToTextEnd(textControlID) {
    var text = document.getElementById(textControlID);
    if (text != null && text.value.length > 0) {
        if (document.selection) {
            var range = document.selection.createRange();
            range.moveStart('character', text.value.length);
            range.collapse();
            range.select();
        }
        else if (window.getSelection) {
            var range = window.getSelection().getRangeAt(0);
            range.setStart(range.startContainer, text.value.length);
            range.setEnd(range.startContainer, text.value.length);

        }
    }
}

function JNumberToThaiBaht(decimalNumber, areaDisplay, controlRefresh) {
    $("#" + decimalNumber).focusout(function () {
        sCurrency = document.getElementById(decimalNumber).value;
        if (sCurrency != "") {
            $.ajax({
                type: "POST",
                url: "../Scripts/SYM_Process.aspx/ThaiBahtToClient",
                data: '{"sCurrency":"' + sCurrency + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    $("#" + areaDisplay).text(data.d);
                    if (controlRefresh != "")
                        document.getElementById(controlRefresh).click();
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText);
                }
            });
        }
        else
            $("#" + areaDisplay).text("");
    });
}

function NumberToValue(id) {
    value = document.getElementById(id).value;
    value = value.replace(/,/g, "");
    return value;
}

//function NumberPointMinus(e, obj, bl_point, bl_minus) {
//	if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode == 8)) {
//		return true;
//	}
//	else {
//		if (e.keyCode == 45 && bl_minus == true) {
//			return CheckRepeatChar(obj, '-');
//		}

//		if (e.keyCode == 46 && bl_point == true) {
//			return CheckRepeatChar(obj, '.');
//		}
//	}
//	return false;
//}

//function CheckRepeatChar(obj, ch_repeat) {
//	if (obj.value.length > 0) { 
//		var i;
//		for (i = 0; i <= obj.value.length - 1; i++) {
//			if (obj.value.charAt(i) == ch_repeat) {
//				return false;
//			}
//		}
//	}
//	return true;
//}